package com.xiaolong.cloud.feign.config;

import com.xiaolong.cloud.feign.adapter.FeignClientLogInterceptor;
import com.xiaolong.cloud.feign.adapter.FeignClintDecoder;
import feign.Feign;
import feign.codec.Decoder;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;

@AutoConfiguration
@ConditionalOnClass({Feign.class})
@EnableFeignClients
@AutoConfigureBefore({FeignAutoConfiguration.class})
public class FeignClientAutoConfig {
    @Bean
    public OkHttpClient okHttpClient() {
        return (new OkHttpClient.Builder()).connectTimeout(60L, TimeUnit.SECONDS).readTimeout(60L, TimeUnit.SECONDS).writeTimeout(120L, TimeUnit.SECONDS).retryOnConnectionFailure(false).connectionPool(new ConnectionPool()).addInterceptor(new FeignClientLogInterceptor()).build();
    }

    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters((Collection)converters.orderedStream().collect(Collectors.toList()));
    }

    @Bean
    public Decoder clientDecoder() {
        return new FeignClintDecoder();
    }
}
